﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

namespace Royaumes_Libres.Database
{
    /// <summary>
    /// 
    /// </summary>
    /// <remarks>Patrick Chabot</remarks>
    public class cUsers
    {
        protected int pPkUser;
        protected string pUsername;
        protected string pEmail;
        protected string pFirstName;
        protected string pLastName;
        protected DateTime pBirthday;
        protected int pFkCountry;
        protected int pFkState;
        protected int pFkCity;
        protected int pFkSexe;
        protected string pLangue;
        protected int pActiveWorld;
        protected DateTime pDateRegistered;
        protected DateTime pLastConnection;
        protected bool pInfoPopUp;
        protected bool pActive;
        protected bool pAdmin;

        /// <summary>
        /// Read-Only: PkUser.
        /// </summary>
        public int PkUser { get { return this.pPkUser; } }
        /// <summary>
        /// Read-Only: Username.
        /// </summary>
        public string Username { get { return this.pUsername; } }
        /// <summary>
        /// Read-Only: Email.
        /// </summary>
        public string Email { get { return this.pEmail; } }
        /// <summary>
        /// FisrtName.
        /// </summary>
        public string FirstName { get { return this.pFirstName; } set { this.pFirstName = value; } }
        /// <summary>
        /// LastName.
        /// </summary>
        public string LastName { get { return this.pLastName; } set { this.pLastName = value; } }
        /// <summary>
        /// Birthday.
        /// </summary>
        public DateTime Birthday { get { return this.pBirthday; } set { this.pBirthday = value; } }
        /// <summary>
        /// FkCountry.
        /// </summary>
        public int FkCountry { get { return this.pFkCountry; } set { this.pFkCountry = value; } }
        /// <summary>
        /// FkState.
        /// </summary>
        public int FkState { get { return this.pFkState; } set { this.pFkState = value; } }
        /// <summary>
        /// FkCity.
        /// </summary>
        public int FkCity { get { return this.pFkCity; } set { this.pFkCity = value; } }
        /// <summary>
        /// FkSexe.
        /// </summary>
        public int FkSexe { get { return this.pFkSexe; } set { this.pFkSexe = value; } }
        /// <summary>
        /// Langue.
        /// </summary>
        public string Langue { get { return this.pLangue; } set { this.pLangue = value; } }
        /// <summary>
        /// ActiveWorld.
        /// </summary>
        public int ActiveWorld { get { return this.pActiveWorld; } set { this.pActiveWorld = value; } }
        /// <summary>
        /// Read-Only: DateRegistered.
        /// </summary>
        public DateTime DateRegistered { get { return this.pDateRegistered; } }
        /// <summary>
        /// Read-Only: LastConnection.
        /// </summary>
        public DateTime LastConnection { get { return this.pLastConnection; } }
        /// <summary>
        /// InfoPopUp.
        /// </summary>
        public bool InfoPopUp { get { return this.pInfoPopUp; } set { this.pInfoPopUp = value; } }
        /// <summary>
        /// Active.
        /// </summary>
        public bool Active { get { return this.pActive; } set { this.pActive = value; } }
        /// <summary>
        /// Admin.
        /// </summary>
        public bool Admin { get { return this.pAdmin; } set { this.pAdmin = value; } }

        /// <summary>
        /// Instancie un cUsers.
        /// </summary>
        /// <remarks>Patrick Chabot</remarks>
        public cUsers()
        {
            this.pPkUser = 0;
        }

        /// <summary>
        /// Retourne le nom complet de l'usager.
        /// </summary>
        /// <returns>FirstName + " " + LastName</returns>
        /// <remarks>Patrick Chabot</remarks>
        public override string ToString()
        {
            return this.pFirstName + " " + this.pLastName;
        }

        /// <summary>
        /// Enregistre le user. Doit avoir été charger avec GetUser().
        /// </summary>
        /// <returns>Retourne TRUE si sauvegarde réussis, FALSE sinon</returns>
        /// <remarks>Patrick Chabot</remarks>
        public bool SaveUser()
        {
            if (this.pPkUser == 0) { return false; }
            SqlCommand cmd = cSQL.GetDbSiteCommand();

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spUpdateUser";

                cmd.Parameters.Add(cSQL.Param("@PkUser", this.pPkUser, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FisrtName", this.pFirstName, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@LastName", this.pLastName, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@Birthday", this.pBirthday, SqlDbType.DateTime));
                cmd.Parameters.Add(cSQL.Param("@FkCountry", this.pFkCountry, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FkState", this.pFkState, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FkCity", this.pFkCity, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FkSexe", this.pFkSexe, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@Langue", this.pLangue, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@InfoPopUp", this.pInfoPopUp, SqlDbType.Bit));
                cmd.Parameters.Add(cSQL.Param("@Active", this.pActive, SqlDbType.Bit));

                int result = cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cUsers.SaveUser(): " + ex.Message);
            }
        }

        /// <summary>
        /// Pour connecter un user
        /// </summary>
        /// <param name="Username">Username</param>
        /// <param name="Password">Password</param>
        /// <returns>cUsers ou NULL si inexistant ou erreur de username ou password</returns>
        /// <remarks>Patrick Chabot</remarks>
        public static cUsers GetUser(string Username, string Password)
        {
            SqlCommand cmd = cSQL.GetDbSiteCommand();

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spGetUser";

                cmd.Parameters.Add(cSQL.Param("@Username", Username, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@Password", Password, SqlDbType.VarChar));

                SqlDataReader dr = cmd.ExecuteReader();
                cUsers user = null;

                dr.Read();

                if (dr.GetInt32(0) != -1)
                {
                    user = new cUsers();

                    user.pPkUser = dr.GetInt32(0);
                    user.pUsername = dr.GetString(1);
                    user.pEmail = dr.GetString(2);
                    user.pFirstName = dr.GetString(3);
                    user.pLastName = dr.GetString(4);
                    user.pBirthday = dr.GetDateTime(5);
                    user.pFkCountry = dr.GetInt32(6);
                    user.pFkState = dr.GetInt32(7);
                    user.pFkCity = dr.GetInt32(8);
                    user.pFkSexe = dr.GetInt32(9);
                    user.pLangue = dr.GetString(10);
                    user.pDateRegistered = dr.GetDateTime(11);
                    user.pLastConnection = dr.GetDateTime(12);
                    user.pInfoPopUp = dr.GetBoolean(13);
                    user.pActive = dr.GetBoolean(14);
                    user.pAdmin = dr.GetBoolean(15);
                }
                return user;
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cUsers.GetUser(string Username, string Password): " + ex.Message);
            }
        }

        /// <summary>
        /// Pour créer un nouveau user.
        /// </summary>
        /// <param name="Username">Username</param>
        /// <param name="Password">Password</param>
        /// <param name="Email">Email</param>
        /// <param name="FisrtName">FisrtName</param>
        /// <param name="LastName">LastName</param>
        /// <param name="Birthday">Birthday</param>
        /// <param name="FkCountry">FkCountry</param>
        /// <param name="FkState">FkState</param>
        /// <param name="FkCity">FkCity</param>
        /// <param name="FkSexe">FkSexe</param>
        /// <param name="Language">Language</param>
        /// <returns>True si la création est réussis.</returns>
        /// <remarks>Patrick Chabot</remarks>
        public static bool NewUser(string Username, string Password, string Email, string FisrtName, string LastName, DateTime Birthday, int FkCountry, int FkState, int FkCity, int FkSexe, string Language)
        {
            SqlCommand cmd = cSQL.GetDbSiteCommand();

            try
            {
                cmd.Connection.Open();
                cmd.CommandText = "cde.spNewUser";

                cmd.Parameters.Add(cSQL.Param("@Username", Username, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@Password", Password, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@Email", Email, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@FisrtName", FisrtName, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@LastName", LastName, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@Birthday", Birthday, SqlDbType.DateTime));
                cmd.Parameters.Add(cSQL.Param("@FkCountry", FkCountry, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FkState", FkState, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FkCity", FkCity, SqlDbType.Int));
                cmd.Parameters.Add(cSQL.Param("@FkSexe", FkSexe, SqlDbType.VarChar));
                cmd.Parameters.Add(cSQL.Param("@Langue", Language, SqlDbType.VarChar));

                int result = cmd.ExecuteNonQuery();

                if (result == 1) { return true; }
                return false;
            }
            catch (Exception ex)
            {
                cmd.Connection.Close();
                throw new Exception("cUsers.NewUser(string Username, string Password, string Email, string FisrtName, string LastName, DateTime Birthday, int FkCountry, int FkState, int FkCity, int FkSexe, string Language): " + ex.Message);
            }
        }
    }
}